package com.zmwx.subject.appliacion.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Author： zmwx
 * @Date： 2024/4/8 18:40
 * @Describe： Feign拦截器
 */
@Component
public class FeignRequestInterceptor  implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //从当前请求的上下文获取
        ServletRequestAttributes requestAttributes=
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=requestAttributes.getRequest();
        if (Objects.nonNull(request)){
            String loginId=request.getHeader("loginId");
            if (!StringUtils.isEmpty(loginId)){
                requestTemplate.header("loginId",loginId);
            }
        }
   }
}
